package com.eshop.modules.user.service.impl;

import java.lang.invoke.SerializedLambda;
import java.io.IOException;
import com.eshop.utils.FileUtil;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.eshop.common.utils.QueryHelpPlus;
import com.eshop.modules.user.service.dto.SystemUserLevelDto;
import java.util.LinkedHashMap;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.user.service.dto.SystemUserLevelQueryCriteria;
import com.eshop.modules.user.service.dto.TaskDto;
import com.eshop.modules.user.domain.UserLevel;
import com.eshop.modules.user.vo.SystemUserLevelQueryVo;
import com.eshop.api.EshopException;
import com.eshop.modules.user.service.dto.UserLevelDto;
import java.util.Iterator;
import java.util.List;
import cn.hutool.core.util.ObjectUtil;
import com.eshop.enums.ShopCommonEnum;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.eshop.modules.user.service.SystemUserTaskService;
import com.eshop.modules.user.service.UserLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.user.service.SystemUserLevelService;
import com.eshop.modules.shop.domain.SystemUserLevel;
import com.eshop.modules.user.service.mapper.SystemUserLevelMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class SystemUserLevelServiceImpl extends BaseServiceImpl<SystemUserLevelMapper, SystemUserLevel> implements SystemUserLevelService
{
    @Autowired
    private IGenerator generator;
    @Autowired
    private SystemUserLevelMapper yxSystemUserLevelMapper;
    @Autowired
    private UserLevelService userLevelService;
    @Autowired
    private SystemUserTaskService systemUserTaskService;
    
    public int getNextLevelId(final int levelId) {
        final List<SystemUserLevel> list = (List<SystemUserLevel>)((LambdaQueryChainWrapper)(this.lambdaQuery().eq(SystemUserLevel::getIsShow, ShopCommonEnum.SHOW_1.getValue())).orderByAsc(SystemUserLevel::getGrade)).list();
        int grade = 0;
        for (final SystemUserLevel userLevel : list) {
            if (userLevel.getId() == levelId) {
                grade = userLevel.getGrade();
            }
        }
        final SystemUserLevel userLevel2 = ((((this.lambdaQuery().eq(SystemUserLevel::getIsShow, ShopCommonEnum.SHOW_1.getValue())).orderByAsc(SystemUserLevel::getGrade)).gt(SystemUserLevel::getGrade, grade)).last("limit 1")).one();
        if (ObjectUtil.isNull(userLevel2)) {
            return 0;
        }
        return userLevel2.getId();
    }
    
    public UserLevelDto getLevelInfo(final Long uid) {
        int levelId = 0;
        final UserLevel userLevel = this.userLevelService.getUserLevel(uid, null);
        if (userLevel != null) {
            levelId = userLevel.getLevelId();
        }
        final List<SystemUserLevelQueryVo> list = this.getLevelListAndGrade(levelId);
        if (list.isEmpty()) {
            throw new EshopException("请后台设置会员等级");
        }
        final TaskDto taskDTO = this.systemUserTaskService.getTaskList(list.get(0).getId(), uid);
        final UserLevelDto userLevelDTO = new UserLevelDto();
        userLevelDTO.setList(list);
        userLevelDTO.setTask(taskDTO);
        return userLevelDTO;
    }
    
    private List<SystemUserLevelQueryVo> getLevelListAndGrade(final Integer levelId) {
        Integer grade = 0;
        final List<SystemUserLevel> list = (List<SystemUserLevel>)((this.lambdaQuery().eq(SystemUserLevel::getIsShow, ShopCommonEnum.SHOW_1.getValue())).orderByAsc(SystemUserLevel::getGrade)).list();
        final List<SystemUserLevelQueryVo> newList = (List<SystemUserLevelQueryVo>)this.generator.convert((List)list, SystemUserLevelQueryVo.class);
        for (final SystemUserLevelQueryVo userLevelQueryVo : newList) {
            if (userLevelQueryVo.getId().compareTo(levelId) == 0) {
                grade = userLevelQueryVo.getGrade();
            }
            if (grade.compareTo(userLevelQueryVo.getGrade()) < 0) {
                userLevelQueryVo.setIsClear(true);
            }
            else {
                userLevelQueryVo.setIsClear(false);
            }
        }
        return newList;
    }
    
    public Map<String, Object> queryAll(final SystemUserLevelQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<SystemUserLevel> page = (PageInfo<SystemUserLevel>)new PageInfo(this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", this.generator.convert(page.getList(), (Class)SystemUserLevelDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<SystemUserLevel> queryAll(final SystemUserLevelQueryCriteria criteria) {
        return (List<SystemUserLevel>)(this.baseMapper).selectList(QueryHelpPlus.getPredicate(SystemUserLevel.class, criteria));
    }
    
    public void download(final List<SystemUserLevelDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final SystemUserLevelDto yxSystemUserLevel : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("商户id", yxSystemUserLevel.getMerId());
            map.put("会员名称", yxSystemUserLevel.getName());
            map.put("购买金额", yxSystemUserLevel.getMoney());
            map.put("有效时间", yxSystemUserLevel.getValidDate());
            map.put("是否为永久会员", yxSystemUserLevel.getIsForever());
            map.put("是否购买,1=购买,0=不购买", yxSystemUserLevel.getIsPay());
            map.put("是否显示 1=显示,0=隐藏", yxSystemUserLevel.getIsShow());
            map.put("会员等级", yxSystemUserLevel.getGrade());
            map.put("享受折扣", yxSystemUserLevel.getDiscount());
            map.put("会员卡背景", yxSystemUserLevel.getImage());
            map.put("会员图标", yxSystemUserLevel.getIcon());
            map.put("说明", yxSystemUserLevel.getExplain());
            map.put("添加时间", yxSystemUserLevel.getAddTime());
            map.put("是否删除.1=删除,0=未删除", yxSystemUserLevel.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
}
